
! 文件的基本操作，
!提示输入文件名，且包含若干数据
!输出文件里的内容

program read_file
  implicit none
  character(len=20) :: filename
  integer :: status, nvals = 0
  real :: value

  write(*,*) 'input file name:'
  read(*,*) filename
  write(*,100) filename
100 format(' ','input file name ', A)

  open(unit=3, file=filename, status='old', action='read', iostat=status, form='formatted')
  openif: if(status == 0) then
     readloop: do
        read(3,*,iostat=status) value
        if(status /= 0) exit
        nvals = nvals + 1
        write(*,110) nvals, value
110     format(' ', 'Line ', I6, ':vlaue = ', F10.4)
     end do readloop

     readif: if(status > 0) then
        write(*,*) 'an error occured when read value'
     else
        write(*,120) nvals
120     format(' ', 'end , there are ', I6, ' values in file')
     end if readif

  else openif
     write(*,130) status
130  format(' ', 'error open file: iostat = ', I6)
  end if openif
  close(unit=3)
end program read_file
